package com.opencee.common.constants;

import org.springframework.http.HttpStatus;

/**
 * 自定义返回码和Http状态码
 * 其他业务码,直接通过BaseFailException抛出即可
 *
 * @author admin
 */

public enum ResultCode {
    // 成功
    OK(20000, HttpStatus.OK, "ResultCode.OK"),
    // 错误提示:2000
    FAILED(20001, HttpStatus.OK, "ResultCode.FAILED"),

    BAD_REQUEST(4000, HttpStatus.BAD_REQUEST, "ResultCode.BAD_REQUEST"),
    SIGNATURE_DENIED(4001, HttpStatus.BAD_REQUEST, "ResultCode.SIGNATURE_DENIED"),

    UNAUTHORIZED(4010, HttpStatus.UNAUTHORIZED, "ResultCode.UNAUTHORIZED"),
    BAD_CREDENTIALS(4011, HttpStatus.OK, "AbstractUserDetailsAuthenticationProvider.badCredentials"),
    ACCOUNT_DISABLED(4012, HttpStatus.OK, "AbstractUserDetailsAuthenticationProvider.disabled"),
    ACCOUNT_EXPIRED(4013, HttpStatus.OK, "AbstractUserDetailsAuthenticationProvider.expired"),
    ACCOUNT_CREDENTIALS_EXPIRED(4014, HttpStatus.OK, "AbstractUserDetailsAuthenticationProvider.credentialsExpired"),
    ACCOUNT_LOCKED(4015, HttpStatus.OK, "AbstractUserDetailsAuthenticationProvider.locked"),

    ACCESS_DENIED(4031, HttpStatus.FORBIDDEN, "AbstractAccessDecisionManager.accessDenied"),
    ACCESS_DENIED_BLACK_LIMITED(4032, HttpStatus.FORBIDDEN, "ResultCode.ACCESS_DENIED_BLACK_LIMITED"),
    ACCESS_DENIED_WHITE_LIMITED(4033, HttpStatus.FORBIDDEN, "ResultCode.ACCESS_DENIED_WHITE_LIMITED"),

    NOT_FOUND(4040, HttpStatus.NOT_FOUND, "ResultCode.NOT_FOUND"),
    METHOD_NOT_ALLOWED(4050, HttpStatus.METHOD_NOT_ALLOWED, "ResultCode.METHOD_NOT_ALLOWED"),
    MEDIA_TYPE_NOT_ACCEPTABLE(4060, HttpStatus.NOT_ACCEPTABLE, "ResultCode.MEDIA_TYPE_NOT_ACCEPTABLE"),
    TOO_MANY_REQUESTS(4290, HttpStatus.TOO_MANY_REQUESTS, "ResultCode.TOO_MANY_REQUESTS"),

    INTERNAL_SERVER_ERROR(5000, HttpStatus.INTERNAL_SERVER_ERROR, "ResultCode.INTERNAL_SERVER_ERROR"),
    SERVICE_UNAVAILABLE(5030, HttpStatus.SERVICE_UNAVAILABLE, "ResultCode.SERVICE_UNAVAILABLE"),
    GATEWAY_TIMEOUT(5040, HttpStatus.GATEWAY_TIMEOUT, "ResultCode.GATEWAY_TIMEOUT");


    private int code;
    private String messageCode;
    private HttpStatus httpStatus;

    ResultCode() {
    }

    ResultCode(int code, HttpStatus httpStatus, String messageCode) {
        this.code = code;
        this.messageCode = messageCode;
        this.httpStatus = httpStatus;
    }

    public static ResultCode getResultEnum(HttpStatus httpStatus) {
        for (ResultCode type : ResultCode.values()) {
            if (type.getHttpStatus().value() == httpStatus.value()) {
                return type;
            }
        }
        return INTERNAL_SERVER_ERROR;
    }

    public static ResultCode valueOfCode(int code) {
        for (ResultCode type : ResultCode.values()) {
            if (type.getCode() == code) {
                return type;
            }
        }
        return INTERNAL_SERVER_ERROR;
    }

    public static ResultCode valueOfMessage(String messageCode) {
        for (ResultCode type : ResultCode.values()) {
            if (type.getMessageCode().equals(messageCode)) {
                return type;
            }
        }
        return INTERNAL_SERVER_ERROR;
    }

    public HttpStatus getHttpStatus() {
        return httpStatus;
    }

    public void setHttpStatus(HttpStatus httpStatus) {
        this.httpStatus = httpStatus;
    }

    public int getCode() {
        return code;
    }

    public String getMessageCode() {
        return messageCode;
    }


    public void setCode(int code) {
        this.code = code;
    }

    public void setMessageCode(String messageCode) {
        this.messageCode = messageCode;
    }
}
